package com.idbk.chargestation.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.idbk.chargestation.R;
import com.idbk.chargestation.activity.user.ActivityRecordDetail;
import com.idbk.chargestation.bean.JsonMywalletRecord;

import java.util.List;
import java.util.Locale;

/**
 * 记录适配器
 * @author zhx
 *
 */
public class MywalletAdapter extends BaseAdapter implements OnItemClickListener{

	public final static String TRADING_LOG_ID = "tradingLogId";
	public final static String DETAIL_TYPE = "detailType";
	private Context context;

	private List<JsonMywalletRecord> mList;

	private LayoutInflater mInflater;

	public MywalletAdapter(
			Context context,
			List<JsonMywalletRecord> list,
			PullToRefreshListView mListView){
		this.context = context;

		mList = list;

		mInflater = LayoutInflater.from(context);

		mListView.setOnItemClickListener(this);
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {

		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final JsonMywalletRecord jmr = mList.get(position);
		ItemView item;
		if (convertView == null) {
			item = new ItemView();
			convertView = mInflater.inflate(R.layout.item_mywallet_layout, parent, false);
			//实例化组件
			item.tradingType =(TextView)convertView.findViewById(R.id.item_message_title);
			item.tradingMoney = (TextView)convertView.findViewById(R.id.item_message_money);
			item.tradingTime =(TextView)convertView.findViewById(R.id.item_message_time);
			//缓存组件
			convertView.setTag(item);
		} else {
			item = (ItemView)convertView.getTag();
		}

		String type,tradingType;
		item.tradingTime.setText(jmr.recordTime);
		if( jmr.tradingType == JsonMywalletRecord.CONSUMPTION) {
			type = "-";
		} else if(jmr.tradingType == JsonMywalletRecord.RECHARGE
				|| jmr.tradingType == JsonMywalletRecord.PRESENTED) {
			type = "+";
		 }else {
			type = "-";
		}
		
		switch (jmr.detailType) {
		case JsonMywalletRecord.CHARGE_CONSUMPTION:
			tradingType = "充电消费";
			break;
		case JsonMywalletRecord.PREPARED_CONSUMPTION:
			tradingType = "预约消费";
			break;
		case JsonMywalletRecord.ALIPAY_RECHARGE:
			tradingType = "支付宝充值";
			break;
		case JsonMywalletRecord.WECHAT_RECHARGE:
			tradingType = "微信充值";
			break;
		case JsonMywalletRecord.BACKGROUND_PRESENTED:
			tradingType = "后台赠送";
			break;
		case JsonMywalletRecord.RECHARGE_PRESENTED:
			tradingType = "充值赠送";
			break;
		case JsonMywalletRecord.INTRODUCTION_PRESENTED:
			tradingType = "介绍赠送";
			break;
		case JsonMywalletRecord.REGISTRATION_PRESENTED:
			tradingType = "注册赠送";
			break;
		case JsonMywalletRecord.PARKING_CHARGE:
			tradingType = "停车费用";
			break;
		case JsonMywalletRecord.REFUND:
			tradingType = "退款";
			type = "-";
			break;
		default:
			tradingType = "未知类型";
			break;
		}
		item.tradingType.setText(tradingType);
		if (jmr.rechargeStatus == JsonMywalletRecord.RECHARGE_STATUS_FINISH) {
			item.tradingMoney.setText(type + "￥ : " + String.format(Locale.CHINA, "%.2f", jmr.money));
		} else if(jmr.rechargeStatus == JsonMywalletRecord.RECHARGE_STATUS_WAITING) {
			item.tradingMoney.setText("等待支付");
		} else if(jmr.rechargeStatus == JsonMywalletRecord.RECHARGE_STATUS_CANCEL) {
			item.tradingMoney.setText("取消支付");
		}else if(jmr.rechargeStatus == JsonMywalletRecord.RECHARGE_STATUS_UNAVAILABLE) {
			item.tradingMoney.setText("订单失效");
		} else {
			item.tradingMoney.setText("数据出错");
		}
		
		return convertView;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		final JsonMywalletRecord jmr = mList.get(position - 1);
		Intent intent = new Intent(context, ActivityRecordDetail.class);
		Bundle bundle=new Bundle();
		bundle.putInt(TRADING_LOG_ID, jmr.tradingLogId);
		bundle.putInt(DETAIL_TYPE, jmr.detailType);
		intent.putExtras(bundle);
		context.startActivity(intent);
	}

	private class ItemView{
		private TextView tradingType;
		private TextView tradingMoney;
		private TextView tradingTime;
	}

}


